package controllers;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import workerswithdb.IDAOWorker;
import api.controller.IAutoservice;
import api.messages.Messager;
import entitiesfactory.EntitiesFactory;

public class Autoservice implements IAutoservice {

	private IDAOWorker daoWorker;;
	private EntitiesFactory entities_factory = EntitiesFactory.getInstance();
	private SimpleDateFormat formatter = new SimpleDateFormat(
			Messager.DATE_FORMAT);
	private ArrayList<String> exceptionMap = new ArrayList<String>();

	public Autoservice() {
		daoWorker = (IDAOWorker) entities_factory.getObject(IDAOWorker.class);
	}

	public ArrayList<String> getEmptyGarage() {
		return daoWorker.getEmptyGarage();
	}

	public ArrayList<String> SortOrderByBeginningDate() {
		return daoWorker.SortOrderByBeginningDate();
	}

	public ArrayList<String> SortOrderByDeadlineDate() {
		return daoWorker.SortOrderByDeadlineDate();
	}

	public ArrayList<String> SortOrderByFillingDate() {
		return daoWorker.SortOrderByFillingDate();
	}

	public ArrayList<String> SortOrderByPrice() {
		return daoWorker.SortOrderByPrice();
	}

	public ArrayList<String> SortMasterByEmployment() {
		return daoWorker.SortMasterByEmployment();
	}

	public ArrayList<String> SortMasterByName() {
		return daoWorker.SortMasterByName();
	}

	public ArrayList<String> SortMadingOrderByBeginningDate() {
		return daoWorker.SortMadingOrderByBeginningDate();

	}

	public ArrayList<String> SortMadingOrderByDeadlineDate() {
		return daoWorker.SortMadingOrderByDeadlineDate();
	}

	public ArrayList<String> SortMadingOrderByFillinggDate() {
		return daoWorker.SortMadingOrderByFillinggDate();

	}

	public ArrayList<String> SortMadingOrderByPrice() {
		return daoWorker.SortMadingOrderByPrice();

	}

	public ArrayList<String> getOrderByMaster(String parameters) {
		return daoWorker.getOrdersByMaster(parameters);
	}

	public ArrayList<String> getMasterByOrder(String parameters) {
		Integer numberOrder = Integer.parseInt(parameters);
		return daoWorker.getMastersByOrder(numberOrder);
	}

	public ArrayList<String> SortDeletedOrderByPeriodByBeginningDate(
			String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
			return daoWorker
					.SortDeletedOrderByPeriodByBeginningDate(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortCanceledOrderByPeriodByBeginningDate(
			String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
			return daoWorker.SortCanceledOrderByPeriodByBeginningDate(begin,
					end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortMadeOrderByPeriodByBeginningDate(
			String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
			return daoWorker.SortMadeOrderByPeriodByBeginningDate(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortDeletedOrderByPeriodByDeadlineDate(
			String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
			return daoWorker.SortDeletedOrderByPeriodByDeadlineDate(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortCanceledOrderByPeriodByDeadlineDate(
			String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
			return daoWorker
					.SortCanceledOrderByPeriodByDeadlineDate(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortMadeOrderByPeriodByDeadlineDate(
			String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
			return daoWorker.SortMadeOrderByPeriodByDeadlineDate(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortDeletedOrderByPeriodByFillingDate(
			String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
			return daoWorker.SortDeletedOrderByPeriodByFillingDate(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortCanceledOrderByPeriodByFillingDate(
			String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
			return daoWorker.SortCanceledOrderByPeriodByFillingDate(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortMadeOrderByPeriodByFillingDate(
			String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
			return daoWorker.SortMadeOrderByPeriodByFillingDate(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortDeletedOrderByPeriodByPrice(String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
			return daoWorker.SortDeletedOrderByPeriodByPrice(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}

	}

	public ArrayList<String> SortCanceledOrderByPeriodByPrice(String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
			return daoWorker.SortCanceledOrderByPeriodByPrice(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}

	}

	public ArrayList<String> SortMadeOrderByPeriodByPrice(String parameters) {
		String[] arr = new String[2];
		arr = parameters.split(" ");
		try {
			Date begin = formatter.parse(arr[0]);
			Date end = formatter.parse(arr[1]);
			return daoWorker.SortMadeOrderByPeriodByPrice(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}

	}

	public ArrayList<String> getFreePlacesOnDate(String parameters) {
		try {
			Date date = formatter.parse(parameters);
			return daoWorker.getEmptyPlacesOnDate(date);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> getNextFreeDate() {
		return daoWorker.getNextFreeDate();
	}

	public ArrayList<String> addMaster(String parameters) {
		return daoWorker.addMaster(parameters);
	}

	public ArrayList<String> deleteMaster(String parameters) {
		return daoWorker.deleteMaster(parameters);
	}

	public ArrayList<String> addGarage(String parameters) {
		Integer numberGarage = Integer.parseInt(parameters);
		return daoWorker.addGarage(numberGarage);
	}

	public ArrayList<String> deleteGarage(String parameters) {
		Integer numberGarage = Integer.parseInt(parameters);
		return daoWorker.deleteGarage(numberGarage);
	}

	public ArrayList<String> addOrder(String parameters) {
		String[] array = parameters.split(" ");
		Integer orderNumber = Integer.parseInt(array[0]);
		try {
			Date fillingDate = formatter.parse(array[1]);
			Date deadlineDate = formatter.parse(array[2]);
			Date beginingDate = formatter.parse(array[3]);
			Integer price = Integer.parseInt(array[4]);
			String masterName = array[5];
			Integer numberGarage = Integer.parseInt(array[6]);

			return daoWorker.addOrder(orderNumber, fillingDate, deadlineDate,
					beginingDate, price, masterName, numberGarage);

		} catch (ParseException e) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;

		}
	}

	public ArrayList<String> markOrderDeleted(String parameters) {
		Integer numberOrder = Integer.parseInt(parameters);
		return daoWorker.markOrderDeleted(numberOrder);
	}

	public ArrayList<String> markOrderCanceled(String parameters) {
		Integer numberOrder = Integer.parseInt(parameters);
		return daoWorker.markOrderCanceled(numberOrder);
	}

	public ArrayList<String> markOrderMade(String parameters) {
		Integer numberOrder = Integer.parseInt(parameters);
		return daoWorker.markOrderMade(numberOrder);
	}

	public ArrayList<String> ShiftTimeOrder(String parameters) {
		String[] arr = parameters.split(" ");
		try {
			Date newDate = formatter.parse(arr[0]);
			Integer numberOrder = Integer.parseInt(arr[1]);
			return daoWorker.ShiftTimeOrder(newDate, numberOrder);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> cloneOrder(String parameters) {
		Integer numberOrder = Integer.parseInt(parameters);
		return daoWorker.cloneOrder(numberOrder);
	}

	public ArrayList<String> exportGarages(String number_garage) {
		return daoWorker.exportGarages(Integer.parseInt(number_garage));

	}

	public ArrayList<String> exportMasters(String master_name) {
		return daoWorker.exportMasters(master_name);

	}

	public ArrayList<String> exportOrders(String order_number) {
		return daoWorker.exportOrders(Integer.parseInt(order_number));
	}

	public ArrayList<String> importGarages() {
		return daoWorker.importGarages();
	}

	public ArrayList<String> importMasters() {
		return daoWorker.importMasters();
	}

	public ArrayList<String> importOrders() {
		return daoWorker.importOrders();
	}

}
